package dm.ast;

import dm.ast.pro.DmCastFun;
import test.envset.GlobalState;

/**
 * DM的列
 * 表、视图中的列
 * 处于AST中的叶子节点
 */
public class DmColumn extends DmExpression{
    public String name = null;
    public String originalname = null;//当这个列有别名的时候 用来记录原来的name
    public DmColumnType type = null;
    public String tableName = null;  //所属表名
    public int tag = 0;//用来记录是否为自然连接的同名列 0表示不是同名列1表示是同名列
    //如果后期进行fill时，找不到合适的列，那么直接用常量填充
    private DmConstant constantValue = null;
    private DmCastFun castFun = null;

    public DmColumn() {
        isCompleted = false;
        isLeaf = true;
        setComplexity();
        setTargetWidt(1);
        setTargetDept(1);
    }

    public DmColumn(String name, DmColumnType type) {
        this.name = name;
        this.type = type;
        this.tableName = null;
        isCompleted = true;
        isLeaf = true;
        reType = type.dtype;
        setComplexity();
        setTargetWidt(1);
        setTargetDept(1);
    }

    public DmColumn(String name, DmColumnType type , DmTable table) {
        this.name = name;
        this.type = type;
        this.tableName = table.getSelectName();
        isCompleted = true;
        isLeaf = true;
        reType = this.type.dtype;
        setComplexity();
        setTargetWidt(1);
        setTargetDept(1);
    }

    //仅用于构建子查询的查询列，用于外层查询
    public DmColumn(String name, DmDataType rtype) {
        this.name = name;
        reType = rtype;
        this.type = new DmColumnType(rtype);
        this.tableName = null;
        isCompleted = true;
        isLeaf = true;
        setComplexity();
        setTargetWidt(1);
        setTargetDept(1);
    }

    public DmColumn(String tableName, String name ,  DmDataType rtype) {
        this.name = name;
        reType = rtype;
        this.type = new DmColumnType(rtype);
        this.tableName = tableName;
        isCompleted = true;
        isLeaf = true;
        setComplexity();
        setTargetWidt(1);
        setTargetDept(1);
    }

    public String getName(){
        return this.name;
    }

    public String getOriginalName(){
        return this.originalname;
    }

    public void setInfo(DmColumn col){
        name = col.name;
        type = col.type;
        tableName = col.tableName;
        tag = col.tag;
    }

    @Override
    public void setWidt(){
        wid_t = 1;
    }

    @Override
    public void setDept(){
        dep_t = 1;
    }

    //模式名，表名，列名
    public String getFullName(){
        if(tableName == null){
            return name;
        }else {
            return tableName + "." + name;
        }
    }

    @Override
    public String toSql(){
        if(constantValue == null){
            if(name == null && castFun == null){
                return " " + this.getClass().getSimpleName();
            }else if(castFun != null){
                return castFun.toSql();
            }

            if(tag == 1){ //如果是自然连接的同名列，直接返回name
                return name;
            }else {
                return getFullName();
            }

        }else {
            return constantValue.toSql();
        }
    }

    @Override
    public void fill(GlobalState globalState){
        if(!isCompleted){
            DmExpression tmp = globalState.getRanCol(reType);
            if(tmp instanceof DmConstant){
                constantValue = (DmConstant) tmp;
            }else if(tmp instanceof DmCastFun){
                castFun = (DmCastFun)tmp;
                ((DmCastFun)tmp).setCastInfo((DmCastFun) tmp);
            }else {
                setInfo((DmColumn) tmp);
            }
            isCompleted = true;
        }
    }



}
